package com.it;

import java.util.Random;
import java.util.Scanner;


public class StatisticalString {
    public static void main(String[] args) {

        String s = generateRandomString(10);
        System.out.println(s.toString());
//        statisticsFrequency();


    }

    /**
     * 生成指定长度的随机字符串，包含大小写字母和数字
     *
     * @param length 随机字符串的长度
     * @return 生成的随机字符串
     */
    public static String generateRandomString(int length) {
        // 定义字符集，包括大小写字母和数字
        char[] characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
        StringBuilder result = new StringBuilder();
        Random random = new Random();

        // 循环生成随机字符，直到达到指定的长度
        for (int i = 0; i < length; i++) {
            // 从字符集中随机选择一个字符
            char character = characters[random.nextInt(characters.length)];
            // 将随机字符添加到结果字符串
            result.append(character);
        }

        // 返回生成的随机字符串
        return result.toString();
    }

    public static void statisticsFrequency() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的字符串");
        int count1 = 0;//A-Z
        int count2 = 0;//a-z
        int count3 = 0;//0-9
        String next = scanner.next();
        for (int i = 0; i < next.length(); i++) {
            char c = next.charAt(i);//获取当前字符
            int ascii = (int) c;//把字符串转成对应的ASCII码值

            if (ascii >= 65 && ascii <= 90) {
                count1++;
            }
            if (ascii >= 97 && ascii <= 122) {
                count2++;
            }
            if (ascii >= 48 && ascii <= 57) {
                count3++;
            }
        }
        System.out.println("大写字母" + count1 + "个");
        System.out.println("小写字母" + count2 + "个");
        System.out.println("数字" + count3 + "个");
    }
}
